package models.users

import javax.inject.{Inject, Singleton}
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.jdbc.JdbcProfile

import scala.concurrent.{ExecutionContext, Future}

@Singleton
class UsersRepository @Inject()(protected val dbConfigProvider: DatabaseConfigProvider,
                                ugRepo: UserGroupRepository)(implicit executionContext: ExecutionContext)
  extends HasDatabaseConfigProvider[JdbcProfile] {

  import profile.api._

  private val ugTable = ugRepo.ugTable
  private val groupTable = ugRepo.groupTable
  private val accountTable = ugRepo.accountTable
  private val userInfoTable = ugRepo.userInfoTable
  private val nasGroupTable = ugRepo.nasGroupTable
  private val nasUserTable = ugRepo.nasUserTable

  def accountsListAll(name: String, page: Int, limit: Int, id: Option[Int] = None, active: Int = 0) = {
    val offset = (page) * limit
    val f = accountTable
      .filterIf(name.nonEmpty)(_.username like s"%$name%")
      .filterOpt(id)(_.id === _)
      .filterIf(active == 0)(_.is_active =!= active)
    val res = f.sortBy(_.id).drop(offset).take(limit).map(f => f).result
    val total = f.length.result
    db.run(res).zip(db.run(total))
  }

  def usersListAll(name: String, page: Int, limit: Int, active: Boolean) = {
    val offset = page * limit
    val f = userInfoTable.filterIf(name.nonEmpty)(_.username like s"%$name%").filterIf(active)(_.is_active > 0)
    val total = f.length.result
    val res = f.sortBy(_.id).drop(offset).take(limit).map(f => f)
    db.run(res.result).zip(db.run(total))
  }

  def groupListAll(name: String, page: Int, limit: Int, company: Int = 0) = {
    val offset = page * limit
    val f = groupTable.filterIf(name.nonEmpty)(_.pname like s"%$name%").filterIf(company != 0)(_.company === company)
    val res = f.sortBy(_.id).drop(offset).take(limit).map(f => f).result
    val total = f.length.result
    db.run(res).zip(db.run(total))
  }

  def nasGroupAll = db.run(nasGroupTable.map(f => f).result)

  def accountSetPermission() = {
    db.run {
      // 全组织
      userInfoTable
        .filter(_.is_active =!= 0)
        .groupBy(_.accountId)
        .map(f =>
          (f._1, f._2.map(a => a.is_active).size)
        ).filter(_._2 > 1).map(f => f._1).result
    }.flatMap {
      userinfo => {
        db.run {
          accountTable
            .filter(_.is_active =!= 2)
            .filter(_.id inSet (userinfo))
            .map(f => f.is_active).update(2) // 参见 PermissionAccess.access_permissions
        }
      }
    }
    db.run {
      // 五羊
      userInfoTable
        .filter(_.is_active =!= 0)
        .filter(_.company === 101)
        .map(f => f.accountId).result
    }.flatMap { userinfo =>
      db.run {
        accountTable
          .filter(_.is_active =!= 2)
          .filter(_.is_active =!= 3)
          .filter(_.id inSet (userinfo))
          .map(f => f.is_active).update(3) // 参见 PermissionAccess.access_permissions
      }
    }
    db.run {
      // 礼顿
      userInfoTable
        .filter(_.is_active =!= 0)
        .filter(_.company === 201)
        .map(f => f.accountId).result
    }.flatMap { userinfo =>
      db.run {
        accountTable
          .filter(_.is_active =!= 2)
          .filter(_.is_active =!= 4)
          .filter(_.id inSet (userinfo))
          .map(f => f.is_active).update(4) // 参见 PermissionAccess.access_permissions
      }
    }
    db.run {
      // 礼顿
      userInfoTable
        .filter(_.is_active =!= 0)
        .filter(_.company === 104)
        .map(f => f.accountId).result
    }.flatMap { userinfo =>
      db.run {
        accountTable
          .filter(_.is_active =!= 2)
          .filter(_.is_active =!= 5)
          .filter(_.id inSet (userinfo))
          .map(f => f.is_active).update(5) // 参见 PermissionAccess.access_permissions
      }
    }
  }


}
